package com.example.demo.util;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.net.URI;

/**
 *
 */
public class ResultUtil {
    public static <T> ResponseEntity<R<T>> success(T data) {


        return ResponseEntity.ok(R.<T>builder()
                .code(HttpStatus.OK.value())
                .msg(HttpStatus.OK.getReasonPhrase())
                .data(data)
                .errorCode(0L)
                .build());
    }

    public static <T> ResponseEntity<R<T>> success(T data, long timestamp) {
        return ResponseEntity.ok(R.<T>builder()
                .code(HttpStatus.OK.value())
                .msg(HttpStatus.OK.getReasonPhrase())
                .data(data)
                .errorCode(0L)
                .build());
    }

    public static <T> ResponseEntity success(URI uri) {
        return ResponseEntity.status(HttpStatus.FOUND).location(uri).build();
    }

    public static <T> ResponseEntity<R<T>> successMsg(String msg) {
        return ResponseEntity.ok(R.<T>builder()
                .code(HttpStatus.OK.value())
                .msg(msg)
                .errorCode(0L)
                .build());
    }

    public static <T> ResponseEntity successOnlyBody(T body) {
        return ResponseEntity.ok(body);
    }

    public static <T> ResponseEntity<R<T>> failMsg(String message, HttpStatus httpStatus) {
        return ResponseEntity.status(HttpStatus.OK).body(R.<T>builder()
                .code(httpStatus.value())
                .msg(message)
                .build());
    }

    public static <T> ResponseEntity<R<T>> failMsg(String message) {
        return ResponseEntity.status(HttpStatus.OK).body(R.<T>builder()
                .code(HttpStatus.BAD_REQUEST.value())
                .msg(message)
                .build());
    }

}
